package com.huikeportal.controller;

import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.DataTypeIndex;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.UserService;

/**
 * 获取权限类接口
 * 
 * @author winner
 * 
 */
@Controller
public class GetPermissionAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private UserService userService;

	@Autowired
	private LoginService loginService;

	/**
	 * 获取字典表信息
	 * 
	 * @param dicKey
	 *            key
	 * @param sysLang
	 *            系统语言
	 * @return
	 */
	@RequestMapping(value = "/huike_getWordBook", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getWordBook(String dicKey, String sysLang) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getWordBook");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (dicKey == null || "".equals(dicKey) || sysLang == null
				|| "".equals(sysLang)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 根据key获取字典表信息
		try {
			List<WordBook> wordBookList = interFaceCommon.getWordBookList(
					sysLang, dicKey);
			if (wordBookList == null || wordBookList.size() == 0) {
				msgResponse.setRespCode("90003");
				msgResponse.setRespCodeDes(MessageTool.RESP_90003);
				// 接口结束时间
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				// 接口状态
				interFaceModel.setInterStatus("success");
				// 记录接口调用情况
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
			// 查询成功
			else {
				msgResponse.setObject(wordBookList);
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 获取用户指标权限
	 * 
	 * @param userId
	 *            用户id
	 * @param indexLevel
	 *            指标级别：0（指标类型）、1（具体指标）
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/huike_getUserIndex", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUserIndex(String userId, String indexLevel) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getUserIndex");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId) || indexLevel == null
				|| "".equals(indexLevel)
				|| (!"0".equals(indexLevel) && !"1".equals(indexLevel))) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 获取信息
		try {
			Map<String, Object> map = userService.selectUserDateType(userId);
			List<DataTypeIndex> indexList = (List<DataTypeIndex>) map
					.get("indexList");
			List<DataTypeIndex> dataTypeList = (List<DataTypeIndex>) map
					.get("dataTypeList");
			// 如果是查询数据类型
			if ("0".equals(indexLevel)) {
				msgResponse.setObject(dataTypeList);
			} else if ("1".equals(indexLevel)) {
				msgResponse.setObject(indexList);
			}
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 根据用户id获取用户信息
	 * 
	 * @param userId
	 * @param source
	 * @return
	 */
	@RequestMapping(value = "/huike_getUserInfo", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUserInfo(String userName, String source) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getUserInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userName == null || "".equals(userName) || source == null
				|| "".equals(source)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			UserInfo user = new UserInfo();
			user.setUserName(userName);
			user.setSource(source);
			UserInfo userInfo = loginService.getUserInfo(user);
			msgResponse.setObject(userInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}
}
